package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.DoMainUtils;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;

/**
 * 碧海银沙
 * @author Persh
 *
 */
public class YinshaNewsAnalyse implements AnalyseNews {

	private static final String[] regex={
		"http://zjphoto.yinsha.com/file/[0-9]{6}/[0-9]*.htm",
		"http://yoka.yinsha.com/.*/[0-9]{4}/[0-9]*.shtml",
		"http://wind.yinsha.com/a/[0-9]*/[0-9]{6}/[0-9]*.htm",
		"http://xcar.yinsha.com/news/[0-9]{6}/news-[0-9]*-1.html",
		"http://[a-z]*.yinsha.com/file/[0-9]*/[0-9]*/[0-9]*.shtml"
	};


	
	public boolean isDetailPage(String url) {
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return false;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();
		if(!isDetailPage(url)){
		}
		news.setUrl(url);
		String title=null;
		String content=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);
		title =doc.select("h2").text();
		if(StringUtils.isBlank(title)){
			title =doc.select("h1").text();
		}
		content=HtmlCleaner.getContentHtml(url,doc.select("div.mntext"));
		if(StringUtils.isBlank(content)){
			content=HtmlCleaner.getContentHtml(url,doc.select("div#viewbody"));
			if(StringUtils.isBlank(content)){
				content=HtmlCleaner.getContentHtml(url,doc.select("div.nr"));
				if(StringUtils.isBlank(content)){
					content=HtmlCleaner.getContentHtml(url,doc.select("div.twlm_sjpcboxc"));
					if(StringUtils.isBlank(content)){
						content=HtmlCleaner.getContentHtml(url,doc.select("div#font_content p"));
						if(StringUtils.isBlank(content)){
							content=HtmlCleaner.getContentHtml(url,doc.select("div#zw"));
							if(StringUtils.isBlank(content))
								content=HtmlCleaner.getContentHtml(url,doc.select("h1.txttle"));
						}
					}
				}
			}
		}
		/**
		 * 初始化、截取 、设置作者信息*******************************************
		 *
		 * */
		String author=null;
		/**
		 *                        	 **************************end *************************
		 *
		 **/
		date=DateUtils.matchDate(doc.select("div.timelist").text());
		if(date==null){
			date=DateUtils.matchDate(doc.select("dl.viewtis dd").text());
			if(date==null){
				date=DateUtils.matchDate(doc.select("div.wzzz").text());
				if(date==null){
					date=DateUtils.matchDate(doc.select("div.twlm_dtime").text());
					if(date==null){
						date=DateUtils.matchDate(doc.select("div#font_content pre").text());
						if(date==null){
							date=DateUtils.matchDate(doc.select("div#xinxi").text());
						}
					}
				}
			}
		}
		if(date == null)
			date=DateUtils.matchDate(doc.select(".detline").text());
		if(StringUtils.isBlank(author)){
			author=JSoupUtils.matchAuthor(doc,  "来源：");
		}
		news.setAuthor(author);
		news.setTitle(StringUtils.trimSpace(title));
		news.setContent(content);
		news.setDate(date);

		return news;
	}

	public static void main(String[] args) {
		YinshaNewsAnalyse analyse=new YinshaNewsAnalyse();
		String url="http://xcar.yinsha.com/news/201306/news-1064902-1.html";
		UrlMeta meta=CrawlHTML.responseToURL(url);
		System.out.println(DoMainUtils.GetDomainName(url));
		System.out.println(analyse.parserHtml(meta));
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	
	public boolean isNeedUpdate(){
		return false;
	}
}
